package 剑指offer;


import java.util.ArrayList;
import java.util.Collections;

/**
 * @Description:
 * @Author: Administrator
 * @Date: 2020-05-04
 */
public class 把数组排成最小的数 {

    public static  String PrintMinNumber(int [] numbers) {

        if (numbers == null && numbers.length== 0) {
            return "";
        }
        ArrayList<String> permutation = Permutation(numbers);
        if (permutation == null && permutation.size() == 0) {
            return null;
        }
        String min = permutation.get(0);

        if (min.startsWith("0")) {
            return min.substring(1);
        }
        return min;
    }
    public static ArrayList<String> Permutation(int[] str) {
        ArrayList<String> list = new ArrayList<>();
        if (str == null && str.length == 0) {
            return list;
        }
        list = per(str, 0,list);
        Collections.sort(list);
        return list;

    }

    public static ArrayList<String> per(int[] arr, int index,ArrayList list) {
        if (index == arr.length - 1) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < arr.length; i++) {
                sb.append(arr[i]);
            }
            list.add(sb.toString());
        }
        for (int i = index; i < arr.length; i++) {
            if (i == index || (i != index && arr[i] != arr[index])) {
                swap(arr, i, index);
                per(arr, index + 1,list);
                swap(arr, i, index);
            }
        }
        return list;

    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    public static void main(String[] args) {

        int[] arr = {3, 32, 321,0};
        System.out.println(PrintMinNumber(arr));



    }


}
